﻿using CommonLib.Core;
using SqlSugar;

namespace WebApiExtend
{
    /// <summary>
    /// 
    /// </summary>
    public class SqlClientBase
    {
        /// <summary>
        /// 当前登录用户ID
        /// </summary>
        public int UserID { get; set; } = 0;

        #region SqlSugarClient 超高性能

        /// <summary>
        /// SqlSugarClient操作数据库是线程安的可以单例
        /// </summary>
        /// <param name="name">读/写</param>
        /// <returns></returns>
        public SqlSugarClient Context()
        {
            //创建数据库对象 (用法和EF Dappper一样通过new保证线程安全)
            //可以在连接字符串中设置连接池pooling=true;表示开启连接池
            //eg:min pool size=2;MaxPoolSize=4;表示最小连接池为2，最大连接池是4；默认是100
            SqlSugarClient DB = new SqlSugarClient(new ConnectionConfig()
            {
                DbType = SqlSugar.DbType.MySql,
                ConnectionString = ConfigurationHelper.configuration.GetSection($"DataBase:ConnString")?.Value,
                //ConnectionString = "server=rm-2ze29it63mpgq7l0iuo.mysql.rds.aliyuncs.com;port=3306;database=pdd;uid=wangshuyu01;pwd=WangShuYu01@654321;CharSet=utf8;Allow User Variables=True;sslMode=None;",
                IsAutoCloseConnection = true,
                InitKeyType = InitKeyType.Attribute //从实体特性中读取主键自增列信息
            },
            db =>
            {
                db.Aop.OnLogExecuting = (sql, pars) =>
                {
                    var _sql = sql;
                    var _pars = pars;

                    //Console.WriteLine(UtilMethods.GetNativeSql(sql, pars));//获取原生SQL推荐 5.1.4.63  性能OK                    
                    //Console.WriteLine(UtilMethods.GetSqlString(DbType.SqlServer,sql,pars))//获取无参数化SQL 对性能有影响，特别大的SQL参数多的，调试使用
                };
            });
            return DB;
        }
        #endregion

        #region SqlSugarScope 性能中上

        /*
         
        /// <summary>
        /// SqlSugarScope操作数据库是线程安的可以单例
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name">读/写</param>
        /// <returns></returns>
        public SqlSugarScope Context<T>(string name = "")
        {
            if (!string.IsNullOrEmpty(name))
            {
                name = $"_{name}";
            }
            var namespaceName = typeof(T).Namespace?.ToLower();
            var sql = new SqlSugarScope(new ConnectionConfig()
            {
                DbType = SqlSugar.DbType.MySql,
                //ConnectionString = ConfigurationHelper.configuration.GetSection($"DataBase:ConnString")?.Value,
                ConnectionString = "server=rm-2ze29it63mpgq7l0iuo.mysql.rds.aliyuncs.com;port=3306;database=pdd;uid=wangshuyu01;pwd=WangShuYu01@654321;CharSet=utf8;Allow User Variables=True;sslMode=None;",
                IsAutoCloseConnection = true,
                InitKeyType = InitKeyType.Attribute //从实体特性中读取主键自增列信息
            },
             db =>
             {
                 db.Aop.OnLogExecuting = (sql, pars) =>
                 {
                     var _sql = sql;
                     var _pars = pars;
                 };
             });
            return sql;
        }
        */
        #endregion
    }
}
